package com.practice.ajCollections010.set.treeset;

import java.util.Iterator;
import java.util.TreeSet;

/**
 * TreeSet 遍历
 */
public class Traversal {
    public static void main(String[] args) {
        TreeSet<Integer> treeSet = new TreeSet<>();
        for (int i = 0; i < 10; i++) {
            treeSet.add(i);
        }

        // 遍历方式一：foreach
        for (Integer item : treeSet) {
            // do something
            System.out.println(item);
        }

        // 方式二：Iterator 迭代器
        System.out.println("====================");
        Iterator<Integer> iterator = treeSet.iterator();
        while (iterator.hasNext()) {
            // do something
            System.out.println(iterator.next());
        }
        // 正序遍历
        System.out.println("====================");
        for (Iterator iter = treeSet.iterator(); iter.hasNext(); ) {
            // do something
            System.out.println(iter.next());
        }
        // 逆序遍历
        System.out.println("====================");
        for (Iterator iter = treeSet.descendingIterator(); iter.hasNext(); ) {
            // do something
            System.out.println(iter.next());
        }

    }
}
